package algoritmoHiru;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

public class TaskA extends Thread{
 public void run (){
	 try {
		Thread.sleep(Hasieraketa.zenbakiKonstantea);

	byte[] mezua=null;
	DatagramSocket bidaltzeSocketa;
	
		bidaltzeSocketa = new DatagramSocket();

	DatagramPacket paketea=new DatagramPacket(mezua,0);
	paketea.setPort(6666);//PING portua static jarri????
	MakinaBektorea makinak = MakinaBektorea.getInstance();
	String makinaIp;
	 while (true){
		 if (Hasieraketa.leader.compareTo("")==0){
				for (Enumeration<String> e=makinak.hash.keys();e.hasMoreElements();){
					makinaIp=e.nextElement();
					if (InetAddress.getLocalHost().getHostAddress().compareTo(makinaIp)!=0){//nere buruei ez bialtzeko
						paketea.setAddress(InetAddress.getByAddress(makinaIp.getBytes()));
						bidaltzeSocketa.send(paketea);
					
					System.out.println("mezua bidali da");
					}
				}
		 }
	 }
	} catch (SocketException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	} catch (UnknownHostException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (InterruptedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
 }
}
